//---------------------------------------------------------------------
// Authors: AlexAngelopoulos, Keith Hill, jachymko
//
// Description: Class implementing Get-MountPoint command.
//
// Creation Date: Oct  9, 2006
// Modified Date: Dec 16, 2006
//
//---------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Management.Automation;
using System.Runtime.InteropServices;

namespace Pscx.Commands.FileSystem.Ntfs
{
    using Interop;

    [Cmdlet(VerbsCommon.Remove, PscxNouns.MountPoint)]
    [Description("Removes a mount point, dismounting the current media if any. If used against the root of a fixed drive, removes the drive letter assignment.")]
    [RelatedLinks("Get-MountPoint")]
    public class RemoveMountPointCommand : PSCmdlet
    {
        private string _name;

        [Parameter(Position = 0,
                   HelpMessage ="Name of the mount point for the volume to remove."),
         AcceptsWildcards(false)]
        public string Name
        {
            set { _name = value; }
            get { return _name; }
        } 

        protected override void ProcessRecord()
        {
            if (!System.IO.Directory.Exists(this._name))
            {
                WriteError(PscxErrorRecord.DirectoryNotFoundError(_name));
            }
            else
            {
                _name = ReparsePointHelper.EnsurePathSlash(_name);

                if(!NativeMethods.DeleteVolumeMountPoint(_name))
                {
                    WriteError(PscxErrorRecord.LastWin32Error("DeleteVolumeMountPointFailed", ErrorCategory.NotSpecified, _name));
                }
            }
        }
    }
}
